//
//  PywUnbundAccountSetLoginPwdController.m
//  PywSdk
//
//  Created by smallwave on 16/8/31.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywUnbundAccountSetLoginPwdController.h"
#import "UILabel+Pyw.h"
#import "UIButton+Pyw.h"
#import "PywTextFieldView.h"
#import "PywMasonry.h"
#import "PywMBProgressHUD+Pyw.h"
#import "PywAccountUnbindFromPassportApi.h"
#import "PywUnbundAccountSuccessController.h"
#import "MutableDictionary.h"

@interface PywUnbundAccountSetLoginPwdController ()

@property (nonatomic, strong) UILabel                   *gameAccountLabel;      // 游戏账号Label
@property (nonatomic, strong) PywTextFieldView          *loginPwdText;          // 独立密码Text
@property (nonatomic, strong) UIButton                  *sureBtn;               // 确定设置按钮
@property (nonatomic, strong) UILabel                   *reminderLabel;         // 温馨提示Label

@property (nonatomic, copy) NSString                    *account;               // 传入的游戏账号

@property (nonatomic, strong) PywAccountUnbindFromPassportApi *accountUnbindFromPassportApi;   // 解绑游戏账号

@end

@implementation PywUnbundAccountSetLoginPwdController
KeyBoardHelper(90)

#pragma mark - Init
- (instancetype)initWithGameAccount:(NSString *)account{
    self = [super init];
    if (self) {
        self.account = account;
    }
    return self;
}

#pragma mark - Lift Cycle Method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUp];
}
#pragma mark - prepareUI
- (void)setUp{
    self.title = @"设置登录密码";
    self.navigationItem.rightBarButtonItem = nil;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self addSubviewAndLayoutSubviews];
}
- (void)addSubviewAndLayoutSubviews{
    [self.view addSubview:self.gameAccountLabel];
    [self.view addSubview:self.loginPwdText];
    [self.view addSubview:self.sureBtn];
    [self.view addSubview:self.reminderLabel];
    
    __weak typeof(self) wSelf = self;
    [self.gameAccountLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.view.mas_top).offset(80);
        make.width.equalTo(wSelf.view.mas_width).multipliedBy(0.85);
    }];
    [self.loginPwdText mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.gameAccountLabel.mas_bottom).offset(20);
        make.width.equalTo(wSelf.gameAccountLabel.mas_width);
        make.height.equalTo(@40);
    }];
    [self.sureBtn mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.loginPwdText.mas_bottom).offset(40);
        make.width.height.equalTo(wSelf.loginPwdText);
    }];
    [self.reminderLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.sureBtn.mas_bottom).offset(20);
        make.width.equalTo(wSelf.sureBtn.mas_width);
    }];
}
#pragma mark - Action
- (void)sureSetAbsolutePwdAction{
    [self.view endEditing:YES];
    
    NSString *password = [self.loginPwdText.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (password.length == 0) {
        [PywMBProgressHUD showError:@"请输入密码" toView:[UIApplication sharedApplication].keyWindow];
        return;
    }else if (password.length < 6) {
        [PywMBProgressHUD showError:@"密码不能低于6位！" toView:self.navigationController.view];
        return;
    }else if (password.length > 12){
        [PywMBProgressHUD showError:@"密码不能高于12位！" toView:self.navigationController.view];
        return;
    }
    
    MutableDictionary *params = [self requestSubAccountSetPwdParams];
    [self.accountUnbindFromPassportApi requestAccountUnbindFromPassportWithParams:params success:^(NSString *status, id anyObjct) {
        
        NSString *account = [NSString stringWithFormat:@"%@",anyObjct[@"account"]];
        NSString *account_pwd = [NSString stringWithFormat:@"%@",anyObjct[@"account_pwd"]];
        
        PywUnbundAccountSuccessController *unBindAccountSuccessVC = [[PywUnbundAccountSuccessController alloc] initWithGameAccount:account LoginPwd:account_pwd];
        [self.navigationController pushViewController:unBindAccountSuccessVC animated:YES];
    } fail:^(NSError *error) {
        
    }];
}
/**
 *  子账号设置登录密码  请求需要传入参数
 */
- (MutableDictionary *)requestSubAccountSetPwdParams{
    NSString *account = [self.account stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *account_pwd = [self.loginPwdText.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    MutableDictionary *params = [[MutableDictionary alloc] init];
    [params setObject:account forKey:@"account"];
    [params setObject:account_pwd forKey:@"account_pwd"];
    return params;
}
#pragma mark - Lazy
- (UILabel *)gameAccountLabel{
    if (!_gameAccountLabel) {
        _gameAccountLabel = [UILabel labelWithText:nil
                                      textAlignment:NSTextAlignmentLeft
                                           fontSize:12
                                          textColor:[UIColor blackColor]];
        
        NSString *propmtStr = [NSString stringWithFormat:@"游戏账号：%@",self.account];
        NSMutableAttributedString *propmtMessage = [[NSMutableAttributedString alloc] initWithString:propmtStr];
        [propmtMessage addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0xFFBC00) range:NSMakeRange(5, self.account.length)];
        _gameAccountLabel.attributedText = propmtMessage;
    }
    return _gameAccountLabel;
}
- (PywTextFieldView *)loginPwdText{
    if (!_loginPwdText) {
        _loginPwdText = [PywTextFieldView passwordTextFieldWithLeftImg:@"icon_password_normal"
                                                          TextPlaceholder:@"请输入登录密码(限6-12位)"
                                                          ButtonNormalImg:@"icon_visible_normal"
                                                        ButtonSelectedImg:@"icon_visible_disabled"];
        [_loginPwdText setAccessibilityId:@"bunphone_pwsd"];
    }
    return _loginPwdText;
}
- (UIButton *)sureBtn{
    if (!_sureBtn) {
        _sureBtn = [UIButton buttonWithBackgroundColor:UIColorFromRGB(0xFFBC00)
                                          cornerRadius:20
                                            titleColor:[UIColor whiteColor]
                                    titleLabelFontSize:18
                                                 title:@"立即解绑"];
        
        [_sureBtn addTarget:self action:@selector(sureSetAbsolutePwdAction) forControlEvents:UIControlEventTouchUpInside];
        [_sureBtn setAccessibilityIdentifier:@"bunphone_btn"];
    }
    return _sureBtn;
}
- (UILabel *)reminderLabel{
    if (!_reminderLabel) {
        _reminderLabel = [UILabel labelWithText:@"温馨提示：请牢记账号和密码，如忘记则无法找回！"
                                  textAlignment:NSTextAlignmentCenter
                                       fontSize:12
                                      textColor:UIColorFromRGB(0x333333)];
    }
    return _reminderLabel;
}
- (PywAccountUnbindFromPassportApi *)accountUnbindFromPassportApi{
    if (!_accountUnbindFromPassportApi) {
        _accountUnbindFromPassportApi = [PywAccountUnbindFromPassportApi manager];
    }
    return _accountUnbindFromPassportApi;
}

@end
